﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using Lamex.Interface;
using DevExpress.XtraCharts;

namespace Lamex.mMachine.UI
{
    public partial class frmMAC_Equipment_Detail : frmBaseChild
    {
        DataTable dataTable = new DataTable();
        string colums_xaxis;
        string colums_yaxis;
        string colums_yaxis2;
        int flag;
        public frmMAC_Equipment_Detail()
        {
            InitializeComponent();
        }
        public frmMAC_Equipment_Detail(DataTable dt, string xaxis, string yaxis,string yaxis2,int flag_chart)
        {
            InitializeComponent();
            dataTable = dt;
            colums_xaxis = xaxis;
            colums_yaxis = yaxis;
            colums_yaxis2 = yaxis2;
            flag = flag_chart;
        }
        private void frmMAC_Equipment_Detail_Resize(object sender, EventArgs e)
        {
            splitContainerControl1.SplitterPosition = this.Width / 2;
        }

        private void frmMAC_Equipment_Detail_Load(object sender, EventArgs e)
        {
            try { base.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
            sbtnView_Click(null, null);
        }

        private void sbtnView_Click(object sender, EventArgs e)
        {
            //显示表格
            ShowGridInfo(dataTable);
            //显示图表
            if (flag == 1 || flag == 7)
                Common.ChartControlHelper.setData_Chart(chartControl1, ViewType.Pie, dataTable, colums_xaxis, colums_yaxis);
            else if (flag == 4 || flag == 5 || flag == 6)
                Common.ChartControlHelper.setData_Chart_Bar(chartControl1, dataTable, colums_xaxis, colums_yaxis, flag);
            else
                Common.ChartControlHelper.setData_Chart_BarLine(chartControl1, dataTable, colums_xaxis, colums_yaxis, colums_yaxis2, flag);
        }
        
        private void ShowGridInfo(DataTable dt)
        {
            if (dt.Rows.Count > 0 && dt != null)
            {
                DataTable dtNew = dt.DefaultView.ToTable(false, new string[] { dt.Columns[0].ColumnName, dt.Columns[1].ColumnName, dt.Columns[2].ColumnName });

                gridControl1.DataSource = dtNew;
                gridView1.Appearance.HeaderPanel.TextOptions.HAlignment= DevExpress.Utils.HorzAlignment.Center;
                gridView1.OptionsBehavior.Editable = false;
                gridView1.OptionsView.ColumnAutoWidth = true;

                //设置页脚汇总
                gridView1.OptionsView.ShowFooter = true;
                gridView1.Columns[0].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
                gridView1.Columns[0].SummaryItem.DisplayFormat = "合计：";

                // for (int i = 1; i < gridView1.Columns.Count; i++)
                // {
                gridView1.Columns[1].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                gridView1.Columns[1].SummaryItem.DisplayFormat = "{0:N0}";
                //}

            }
        }

        public override void DoView(IButtonInfo sender)
        {
            sbtnView_Click(null, null);
            base.DoView(sender);
        }
        public override void DoPrint(IButtonInfo sender)
        {
            Common.ExportHelper exportHelper = new Common.ExportHelper();
            exportHelper.PrintPreview(gridControl1, chartControl1);
            base.DoPrint(sender);
        }

        public override void DoExport(IButtonInfo sender)
        {
            Common.ExportHelper exportHelper = new Common.ExportHelper();
            exportHelper.ExportToExcel(gridControl1, chartControl1);
            base.DoExport(sender);
        }

        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }
    }
}